# -*- coding: UTF-8 -*_
from PIL import Image
from pytesseract import *
import requests
from io import BytesIO
def cjzh(s):
    try :
        return float(s)
    except:
        if ('优秀'in s):
            return 100.0
        if ('良好' in s):
            return 85.0
        if ('中等' in s):
            return 75.0
        if ('及格' in s):
            return 65.0
        return 0.0
def jsjd(jd):
    if (jd >= 90):
        return 5.0
    if (jd >= 85):
        return 4.5
    if (jd >= 80):
        return 4.0
    if (jd >= 75):
        return 3.5
    if (jd >= 70):
        return 3.0
    if (jd >= 65):
        return 2.5
    if (jd >= 60):
        return 2.0
    return 0.0
def gs(s):
    s=s.replace(' ','')
    s=s.replace('\t','')
    s=s.replace('\n','')
    return s
def yzm(s):        #s是图片本地路径
    im = Image.open(s)
    #im.show()
    imgry = im.convert('L')#把图片变成灰色
    threshold = 140

    table = []

    for i in range(256):#二值化变成黑白

        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    out = imgry.point(table, '1')
    asd = pytesseract.image_to_string(out,lang='fyx')#然后就是机器识别咯，lang包来自小贩子的喂图调教成果（机器学习）~~~
    return  asd


zh=input('输入账号:\n')
mm=input('输入密码:\n')
img_src='http://202.119.113.135/validateCodeAction.do?random=0.5'#获取验证码的链接
url='http://202.119.113.135/gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm'
url1='http://202.119.113.135/loginAction.do'#登录链接
pm_url='http://202.119.113.135/yxsapplyAction.do?oper=zzy'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/52.0'}#请求头
'''
fw函数是用来验证密码是否正确的函数
mm参数是密码
xc参数是线程编号~~
'''
def dl(zh,mm):#登录
    valcode = requests.get(img_src) #从验证码链接获取图片
    cookies = valcode.cookies.get_dict() #保存cookies
    image = Image.open(BytesIO(valcode.content)) #图片获取
    #path1 = os.path.abspath('.')
    lj='yzm.bmp'#图片保存路径
    try:
        image.save(lj)#保存图片
    except:
        return dl(zh,mm)
    yzm_str = yzm(lj)
    if (len(yzm_str) != 4):#验证码不是4位说明识别出错
        return dl(zh,mm)
    mms = str(mm)
    while (len(mms) < 6):#密码补0
        mms = '0' + mms
    data = {        #需要post的数据
        "zjh": str(zh),
        "mm": mms,
        "v_yzm": yzm_str,
    }
    response = requests.post(url1, data=data, headers=headers, cookies=cookies)#获取网页
    htxt = response.text#网页源代码
    if ('您的密码不正确，请您重新输入' in htxt):

        print('密码错误')
        return '0'
    elif ('你输入的验证码错误' in htxt):
        return dl(zh, mm)
    else:
        datas = {
            'oper': 'zzy',
        }
        response = requests.get(url, headers=headers, cookies=cookies)  # 获取网页
        response1 = requests.post(pm_url, data=datas, headers=headers, cookies=cookies)  # 获取网页
        return [response.text,response1.text]

if __name__ == '__main__':

    [s, s1] = dl(zh, mm)
    if (s == '0'):
        print('导出失败')
    else:
        pass

    kch = ['课程号']  # 课程号
    kxh = ['课序号']  # 课序号
    kcm = ['课程名']  # 课程名
    ywkcm = ['英文课程名']  # 英文课程名
    xf = ['学分']  # 学分
    kcsx = ['课程属性']  # 课程属性
    cj = ['成绩']  # 成绩
    jd = ['绩点']  # 绩点
    bg = [0, kch, kxh, kcm, ywkcm, xf, kcsx, 0, cj, jd]  # 表格
    k = 0
    sx = 0
    xieru = False
    s = s.split('\n')
    gks = 0
    for line in s:
        if xieru:
            if sx == 7:
                sx = 8
                continue
            zj = line.strip()
            if (sx == 8):
                zj = zj.split('&')[0]
                zj = zj.split('>')[1]
                zj = cjzh(zj)
                zj = float(zj)
                if zj < 60:
                    gks += 1
                jd.append(jsjd(zj))

            x = bg[sx]
            x.append(zj)

            xieru = False
            continue
        if 'onMouseOut' in line:
            k += 1
            sx = 0
        if 'td align="' in line and 'center' in line:
            sx += 1
            xieru = True
    '''
    print(kch)
    print(kxh)
    print(kcm)
    print(ywkcm)
    print(xf)
    print(kcsx)
    print(cj)
    print(jd)
    '''
    i = 1
    s1 = s1.split('var jdcj = \'')[1]
    pjjd = s1.split('\'')[0]
    s1 = s1.split('绩点排名')[1]
    jdpm = s1.split('<td width="20%" >')[1]
    jdpm = jdpm.split('</td>')[0]
    s1 = s1.split('平均成绩')[1]
    pjcj = s1.split('<td width="35%" >')[1]
    pjcj = pjcj.split('</td>')[0]
    pjcj = gs(pjcj)
    jdpm = gs(jdpm)
    while i < len(kch):
        print('\n\n第' + str(i) + '门:')
        k = 1
        s = ''
        while k < len(bg):
            if k == 7:
                k = 8

            s = s + bg[k][0] + ':' + str(bg[k][i]) + '\n'
            k += 1
        print(s)
        i += 1
    print('一共有' + str(len(kch) - 1) + '门课程，您一共挂了' + str(gks) + '科~~')
    print('您的加权绩点为:' + str(pjjd))
    print('您的加权成绩为;' + str(pjcj))
    print('您的绩点排名为;' + str(jdpm))
